package com.larissa.android.quiz;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuProvider;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.larissa.android.quiz.databinding.ActivityMainBinding;
import com.larissa.android.quiz.databinding.FragmentMainBinding;
import com.larissa.android.quiz.databinding.FragmentMainLandBinding;

import java.util.List;

public class MainFragment extends Fragment
{
    private MainViewModel mainViewModel;
    private List<Question> questions;
    private FragmentMainBinding fragmentMainBinding;
    private FragmentMainLandBinding fragmentMainLandBinding;
    private Question nowQuestion;
    private Question backQuestion;
    private String tempString;
    private int flag;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mainViewModel=new ViewModelProvider(requireActivity()).get(MainViewModel.class);

        flag = mainViewModel.statusFlag;
        if(mainViewModel.nowQuestion == null)
        {
            mainViewModel.nowQuestion = new Question("", "0");
            nowQuestion = mainViewModel.nowQuestion;
            mainViewModel.nowString = "";
        }
        else
        {
            nowQuestion = mainViewModel.nowQuestion;
            mainViewModel.nowString = nowQuestion.getQuestionText();
        }
        try{
            backQuestion = nowQuestion.clone();
        }catch  (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        super.onCreateView(inflater, container, savedInstanceState);
        int orientation=getResources().getConfiguration().orientation;
        if(orientation== Configuration.ORIENTATION_LANDSCAPE)
        {
            mainViewModel.DirFlag = 1;
            fragmentMainLandBinding = FragmentMainLandBinding.inflate(inflater, container, false);
            return fragmentMainLandBinding.getRoot();
        }
        else
        {
            mainViewModel.DirFlag = 0;
            fragmentMainBinding = FragmentMainBinding.inflate(inflater, container, false);
            return fragmentMainBinding.getRoot();
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(mainViewModel.DirFlag == 0)
        {
            if(savedInstanceState != null)
            {

            }
            fragmentMainBinding.setQuestion(backQuestion);
            fragmentMainBinding.btnNum0.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum1.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum2.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum3.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum4.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum5.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum6.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum7.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum8.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnNum9.setOnClickListener(this::btnnumClick);
            fragmentMainBinding.btnAdd.setOnClickListener(this::btnopClick);
            fragmentMainBinding.btnMult.setOnClickListener(this::btnopClick);
            fragmentMainBinding.btnDev.setOnClickListener(this::btnopClick);
            fragmentMainBinding.btnDot.setOnClickListener(this::btnopClick);
            fragmentMainBinding.btnMod.setOnClickListener(this::btnopClick);
            fragmentMainBinding.btnSub.setOnClickListener(this::btnopClick);
            fragmentMainBinding.btnEq.setOnClickListener(this::btnEqClick);
            fragmentMainBinding.btnAc.setOnClickListener(this::btnAcClick);
            fragmentMainBinding.btnBack.setOnClickListener(this::btnBackClick);
        }
        else if(mainViewModel.DirFlag == 1)
        {
            fragmentMainLandBinding.setQuestion(backQuestion);
            fragmentMainLandBinding.btnNum0.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum1.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum2.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum3.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum4.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum5.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum6.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum7.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum8.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnNum9.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnAdd.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.btnMult.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.btnDev.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.btnDot.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.btnMod.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.btnSub.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.btnEq.setOnClickListener(this::btnEqClick);
            fragmentMainLandBinding.btnAc.setOnClickListener(this::btnAcClick);
            fragmentMainLandBinding.btnBack.setOnClickListener(this::btnBackClick);
            fragmentMainLandBinding.buttonLeftBracket.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.buttonRightBracket.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.btnPow.setOnClickListener(this::btnopClick);
            fragmentMainLandBinding.btnSin.setOnClickListener(this::btnmathClick);
            fragmentMainLandBinding.btnCos.setOnClickListener(this::btnmathClick);
            fragmentMainLandBinding.btnTan.setOnClickListener(this::btnmathClick);
            fragmentMainLandBinding.btnE.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnPi.setOnClickListener(this::btnnumClick);
            fragmentMainLandBinding.btnRad.setOnClickListener(this::btnmathClick);
            fragmentMainLandBinding.btnStep.setOnClickListener(this::btnmathClick);
        }
    }

    private void btnmathClick(View view)
    {
        String tempString;
        Character tempChar;
        switch (view.getId())
        {
            case R.id.btn_sin:mainViewModel.nowString = mainViewModel.nowString+'s'; mainViewModel.BacketCount++;break;
            case R.id.btn_cos:mainViewModel.nowString = mainViewModel.nowString+'c'; mainViewModel.BacketCount++;break;
            case R.id.btn_tan:mainViewModel.nowString = mainViewModel.nowString+'t'; mainViewModel.BacketCount++;break;
            case R.id.btn_rad:mainViewModel.nowString = mainViewModel.nowString+'r'; mainViewModel.BacketCount++;break;
            case R.id.btn_step:mainViewModel.nowString = mainViewModel.nowString+'!'; mainViewModel.BacketCount++;break;
        }
        mainViewModel.lastString = mainViewModel.nowString;
        tempString = mainViewModel.nowString.replaceAll("[s]", "sin(");
        tempString = tempString.replaceAll("[c]", "cos(");
        tempString = tempString.replaceAll("[t]", "tan(");
        tempString = tempString.replaceAll("[r]", "sqrt(");
        mainViewModel.nowQuestion.setQuestionText(tempString);
        if(mainViewModel.DirFlag == 0)
        {
            fragmentMainBinding.setQuestion(mainViewModel.nowQuestion);
        }
        else if(mainViewModel.DirFlag == 1)
        {
            fragmentMainLandBinding.setQuestion(mainViewModel.nowQuestion);
        }
    }

    private void btnBackClick(View view)
    {
        String tempString;
        String tempAnswer;
        Character tempchar;
        CompStack Testcomp = new CompStack("TestString");
        tempString = mainViewModel.nowString;
        mainViewModel.nowString = mainViewModel.nowString.substring(0, mainViewModel.nowString.length()-1);
        do {
            tempString = tempString.substring(0, tempString.length()-1);
            tempchar = tempString.charAt(tempString.length()-1);
        }
        while (!(tempchar >= '0' && tempchar <= '9'));
        tempAnswer = String.valueOf(Testcomp.getAnswer(tempString)).replaceAll("0+?$", "");
        tempAnswer = tempAnswer.replaceAll("[.]$", "");
        nowQuestion.setQuestionAnswer(tempAnswer);
        tempString = tempString.replaceAll("[s]", "sin(");
        tempString = tempString.replaceAll("[c]", "cos(");
        tempString = tempString.replaceAll("[t]", "tan(");
        tempString = tempString.replaceAll("[r]", "sqrt(");
        nowQuestion.setQuestionText(tempString);
        if(mainViewModel.DirFlag == 0)
        {
            fragmentMainBinding.setQuestion(nowQuestion);
        }
        else if(mainViewModel.DirFlag == 1)
        {
            fragmentMainLandBinding.setQuestion(nowQuestion);
        }
    }
    private void btnAcClick(View view)
    {
        mainViewModel.nowString="";
        mainViewModel.nowQuestion.setQuestionText("");
        mainViewModel.nowQuestion.setQuestionAnswer("0");
        if(mainViewModel.DirFlag == 0)
        {
            fragmentMainBinding.setQuestion(mainViewModel.nowQuestion);
        }
        else if(mainViewModel.DirFlag == 1)
        {
            fragmentMainLandBinding.setQuestion(mainViewModel.nowQuestion);
        }
    }
    private void btnEqClick(View view)
    {
        String tempString;
        String tempAnswer;
        CompStack Testcomp = new CompStack("TestString");
        tempString = mainViewModel.nowString.replaceAll("[s]", "sin(");
        tempString = tempString.replaceAll("[c]", "cos(");
        tempString = tempString.replaceAll("[t]", "tan(");
        tempString = tempString.replaceAll("[r]", "sqrt(");
        mainViewModel.nowQuestion.setQuestionText(tempString);
        tempAnswer = String.valueOf(Testcomp.getAnswer(mainViewModel.nowString)).replaceAll("0+?$", "");
        tempAnswer = "=" + tempAnswer.replaceAll("[.]$", "");
        mainViewModel.nowQuestion.setQuestionAnswer(tempAnswer);
        Question tempquestion = new Question(mainViewModel.nowQuestion.getQuestionText(), mainViewModel.nowQuestion.getQuestionAnswer());
        mainViewModel.historyQuestions.add(tempquestion);
        if(mainViewModel.DirFlag == 0)
        {
            fragmentMainBinding.setQuestion(mainViewModel.nowQuestion);
        }
        else if(mainViewModel.DirFlag == 1)
        {
            fragmentMainLandBinding.setQuestion(mainViewModel.nowQuestion);
        }
    }
    private void btnopClick(View view)
    {
        String tempString;
        Character tempChar;
        tempChar = mainViewModel.nowString.charAt(mainViewModel.nowString.length()-1);
        switch (view.getId())
        {
            case R.id.btn_add:
                if(IsOpSet(tempChar))
                    mainViewModel.nowString = mainViewModel.nowString.substring(0, mainViewModel.nowString.length()-1);
                mainViewModel.nowString = mainViewModel.nowString+'+';break;
            case R.id.btn_dev:
                if(IsOpSet(tempChar))
                    mainViewModel.nowString = mainViewModel.nowString.substring(0, mainViewModel.nowString.length()-1);
                mainViewModel.nowString = mainViewModel.nowString+'÷';break;
            case R.id.btn_sub:
                if(IsOpSet(tempChar))
                    mainViewModel.nowString = mainViewModel.nowString.substring(0, mainViewModel.nowString.length()-1);
                mainViewModel.nowString = mainViewModel.nowString+'-';break;
            case R.id.btn_mult:
                if(IsOpSet(tempChar))
                    mainViewModel.nowString = mainViewModel.nowString.substring(0, mainViewModel.nowString.length()-1);
                mainViewModel.nowString = mainViewModel.nowString+'×';break;
            case R.id.btn_mod:
                if(IsOpSet(tempChar))
                    mainViewModel.nowString = mainViewModel.nowString.substring(0, mainViewModel.nowString.length()-1);
                mainViewModel.nowString = mainViewModel.nowString+'%';break;
            case R.id.btn_pow:
                if(IsOpSet(tempChar))
                    mainViewModel.nowString = mainViewModel.nowString.substring(0, mainViewModel.nowString.length()-1);
                mainViewModel.nowString = mainViewModel.nowString+'^';break;
            case R.id.btn_dot:
                if(IsOpSet(tempChar))
                    mainViewModel.nowString = mainViewModel.nowString.substring(0, mainViewModel.nowString.length()-1);
                mainViewModel.nowString = mainViewModel.nowString+'.';break;
            case R.id.button_left_bracket:mainViewModel.nowString = mainViewModel.nowString+'('; mainViewModel.BacketCount++;break;
            case R.id.button_right_bracket:
                if(mainViewModel.BacketCount > 0)
                {
                    mainViewModel.nowString = mainViewModel.nowString + ')';
                    mainViewModel.BacketCount--;
                }
                else
                    mainViewModel.BacketCount=0;
                break;
        }
        mainViewModel.lastString = mainViewModel.nowString;
        tempString = mainViewModel.nowString.replaceAll("[s]", "sin(");
        tempString = tempString.replaceAll("[c]", "cos(");
        tempString = tempString.replaceAll("[t]", "tan(");
        tempString = tempString.replaceAll("[r]", "sqrt(");
        mainViewModel.nowQuestion.setQuestionText(tempString);
        if(mainViewModel.DirFlag == 0)
        {
            fragmentMainBinding.setQuestion(mainViewModel.nowQuestion);
        }
        else if(mainViewModel.DirFlag == 1)
        {
            fragmentMainLandBinding.setQuestion(mainViewModel.nowQuestion);
        }
    }
    private boolean IsOpSet(Character Chardat)
    {
        if((Chardat == '+') || (Chardat == '-') || (Chardat=='×') || (Chardat=='÷') || (Chardat == '%') || (Chardat == '.'))
            return true;
        else
            return false;
    }

    @SuppressLint("NonConstantResourceId")
    private void btnnumClick(View view)
    {
        String tempString;
        int tempCount;
        String tempAnswer;
        CompStack Testcomp = new CompStack("TestString");
        switch (view.getId())
        {
            case R.id.btn_num0: mainViewModel.nowString = mainViewModel.nowString+'0';break;
            case R.id.btn_num1: mainViewModel.nowString = mainViewModel.nowString+'1';break;
            case R.id.btn_num2: mainViewModel.nowString = mainViewModel.nowString+'2';break;
            case R.id.btn_num3: mainViewModel.nowString = mainViewModel.nowString+'3';break;
            case R.id.btn_num4: mainViewModel.nowString = mainViewModel.nowString+'4';break;
            case R.id.btn_num5: mainViewModel.nowString = mainViewModel.nowString+'5';break;
            case R.id.btn_num6: mainViewModel.nowString = mainViewModel.nowString+'6';break;
            case R.id.btn_num7: mainViewModel.nowString = mainViewModel.nowString+'7';break;
            case R.id.btn_num8: mainViewModel.nowString = mainViewModel.nowString+'8';break;
            case R.id.btn_num9: mainViewModel.nowString = mainViewModel.nowString+'9';break;
            case R.id.btn_e:    mainViewModel.nowString = mainViewModel.nowString+"2.718281828";break;
            case R.id.btn_pi:   mainViewModel.nowString = mainViewModel.nowString+"3.141592654";break;
        }
        mainViewModel.lastString = mainViewModel.nowString;
        tempCount = mainViewModel.BacketCount;
        tempString = mainViewModel.nowString;;
        while(tempCount>0)
        {
            tempString = tempString + ')';
            tempCount--;
        }
        tempAnswer = String.valueOf(Testcomp.getAnswer(tempString)).replaceAll("0+?$", "");
        tempString = mainViewModel.nowString.replaceAll("[s]", "sin(");
        tempString = tempString.replaceAll("[c]", "cos(");
        tempString = tempString.replaceAll("[t]", "tan(");
        tempString = tempString.replaceAll("[r]", "sqrt(");
        mainViewModel.nowQuestion.setQuestionText(tempString);
        tempAnswer = tempAnswer.replaceAll("[.]$", "");
        mainViewModel.nowQuestion.setQuestionAnswer(tempAnswer);
        if(mainViewModel.DirFlag == 0)
        {
            fragmentMainBinding.setQuestion(mainViewModel.nowQuestion);
        }
        else if(mainViewModel.DirFlag == 1)
        {
            fragmentMainLandBinding.setQuestion(mainViewModel.nowQuestion);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        fragmentMainBinding=null;
    }

}
